package com.lyy.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author 俞伟
 * @version 1.0
 * @date 2020/8/26
 * @description com.tongsheng.common.utils
 */
public class ConcatUtils {
    /**
     * 拼接指定字符串 flag {true:左拼接，false:右拼接}
     *
     * @param str
     * @param diff
     * @param addStr
     * @return
     */
    public static String appendStr(StringBuffer str, int diff, String addStr, boolean flag) {
        StringBuffer buffer = new StringBuffer();
        if (flag) {
            //左侧添加指定字符串
            if (diff > 0) {
                for (int i = 0; i < diff; i++) {
                    buffer.append(addStr);
                }
            }
            buffer.append(str);
            return buffer.toString();
        } else {
            //右侧添加指定字符串
            if (diff > 0) {
                for (int i = 0; i < diff; i++) {
                    str.append(addStr);
                }
            }
            return str.toString();
        }
    }

    /**
     * 拼接指定字符串 flag {true:左拼接，false:右拼接}
     *
     * @param str
     * @param diff
     * @param addStr
     * @return
     */
    public static String appendStr(String content, int diff, String addStr, boolean flag) {
        StringBuffer str = new StringBuffer(content);
        StringBuffer buffer = new StringBuffer();
        if (flag) {
            //左侧添加指定字符串
            if (diff > 0) {
                for (int i = 0; i < diff; i++) {
                    buffer.append(addStr);
                }
            }
            buffer.append(str);
            return buffer.toString();
        } else {
            //右侧添加指定字符串
            if (diff > 0) {
                for (int i = 0; i < diff; i++) {
                    str.append(addStr);
                }
            }
            return str.toString();
        }
    }

    /**
     * 1,2,3,4,5,6 转 '1','2','3','4','5','6'
     *
     * @param content
     * @return
     */
    public static String splitStrByComma(String content) {
        if (StringUtils.isBlank(content)) {
            return null;
        }
        String[] strings = content.split(",");
        StringBuffer buffer = new StringBuffer();
        if (CommonUtils.isNotEmpty(strings)) {
            for (String single : strings) {
                if (StringUtils.isNotBlank(single)) {
                    buffer.append("'").append(single).append("',");
                }
            }
        }
        buffer.deleteCharAt(buffer.length() - 1);
        return buffer.toString();
    }

    /**
     * 1,2,3,4,5,6 转 '1','2','3','4','5','6'
     *
     * @param content
     * @return
     */
    public static String splitStrByComma(List contents) {
        StringBuffer buffer = new StringBuffer();
        if (CommonUtils.isNotEmpty(contents)) {
            for (Object single : contents) {
                if (CommonUtils.isNotEmpty(single)) {
                    buffer.append("'").append(single).append("',");
                }
            }
        }
        buffer.deleteCharAt(buffer.length() - 1);
        return buffer.toString();
    }

    /**
     * 1,2,3,4,5,6 转 '1','2','3','4','5','6'
     *
     * @param content
     * @return
     */
    public static String splitStrByComma(String[] contents) {
        StringBuffer buffer = new StringBuffer();
        if (CommonUtils.isNotEmpty(contents)) {
            for (String single : contents) {
                if (StringUtils.isNotBlank(single)) {
                    buffer.append("'").append(single).append("',");
                }
            }
        }
        buffer.deleteCharAt(buffer.length() - 1);
        return buffer.toString();
    }

    /**
     * 1,2,3,4,5,6 转 '1','2','3','4','5','6'
     *
     * @param obj 传入 list集合或字符串或String数组
     * @return
     */
    public static String splitByComma(Object obj) {
        if (CommonUtils.isNotEmpty(obj)) {
            if (obj instanceof String) {
                return splitStrByComma(String.valueOf(obj));
            } else if (obj instanceof List) {
                return splitStrByComma((List) obj);
            } else if (obj instanceof String[]) {
                return splitStrByComma((String[]) obj);
            }
        }
        return null;
    }
}
